package com.orange.cloud.common.core.msg;

import com.google.gson.GsonBuilder;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.MessageFormat;

import static com.orange.cloud.common.core.msg.Msg.ERROR_CHECK_100;
import static com.orange.cloud.common.core.msg.Msg.SUCCESS_0;


public class R<T> {

    public static <T> Message success() {
        return success(SUCCESS_0, null);
    }

    /**
     * 成功
     * 并返回对象
     * @param t
     * @param <T>
     * @return
     */
    public static <T> Message success(T t) {
        return success(SUCCESS_0, t);
    }

    /**
     * 成功，自定义消息，并返回对象
     * @param msg
     * @param t
     * @param <T>
     * @return
     */
    public static <T> Message success(Msg msg, T t) {
        return success(msg,null,t);
    }

    public static <T> Message success(Msg msg, String errorMsg, T t) {
        return out(msg.getCode(), msg.getMsg(),errorMsg, t);
    }



    /**
     * 动态返回错误消息
     * @param msgEnum
     * @param param1
     * @param param2
     * @param param3
     * @return
     */
    public static Message error(Msg msgEnum, String param1, String param2, String param3) {
        String[] params=new String[]{param1,param2,param3};
        return error(msgEnum,params);
    }
    public static Message error(Msg msgEnum, String param1, String param2) {
        String[] params=new String[]{param1,param2};
        return error(msgEnum,params);
    }
    public static Message error(Msg msgEnum, String param) {
        String[] params=new String[]{param};
        return error(msgEnum,params);
    }
    public static Message error(Msg msgEnum, String[] params) {
        String msg = msgEnum.getMsg();
        msg = MessageFormat.format(msg, params);
        return error(msgEnum.getCode(),msg, null,null);
    }

    public static Message error() {
        return error("失败");
    }
    public static Message error(Msg msgEnum) {
        return error(msgEnum, "");
    }
    public static Message error(String msg) {
        return error(ERROR_CHECK_100,msg);
    }

    /**
     * 比较特殊
     * 前端展示消息
     * 后端错误定位消息
     * @param msg
     * @param errorMsg
     * @return
     */
    public static Message error(String msg, String errorMsg) {
        return error(ERROR_CHECK_100.getCode(), msg,errorMsg,null);
    }
    public static <T> Message error(Integer code, String msg, T t) {
        return error(code, msg, null,t);
    }
    public static <T> Message error(Integer code, String msg, String errorMsg, T t) {
        return out(code, msg,errorMsg, t);
    }

    public static Message error(Integer code, String msg) {
        return error(code, msg, null,null);
    }

    private static <T> Message out(Integer code, String msg, String errorMsg, T t) {
        Head head = new Head(code, msg,errorMsg);
        return Message.builder().head(head).body(t).build();
    }

    public static void respOut(HttpServletResponse response,Message message) throws IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json; charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.write(new GsonBuilder().create().toJson(message));
    }
}
